/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnlFunctionManagement.java
 *
 * Created on 12-08-2011, 08:25:51
 */
package tuitionmanagement.gui;

import tuitionmanagement.entity.Function;
import tuitionmanagement.DAO.FunctionFee;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author NHAN
 */
public class pnlFunctionManagement extends javax.swing.JPanel {

    Function fun = new Function();
    FunctionFee buz = new FunctionFee();
    int index;
    int selectRow = -1;
    public int confirm;
    ArrayList<Function> lst;
   

    /** Creates new form pnlFunctionManagement */
    public pnlFunctionManagement() {
        initComponents();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        lst = buz.readByAll();
        loadTablefunountDetail(index);
    }

    public void loadTablefunountDetail(ArrayList<Function> lst) {
        Vector vRow = new Vector();
        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();
            fun = lst.get(i);
            v.addElement(fun.getFunctionId());
            v.addElement(fun.getFunctionName());
            v.addElement(fun.getDescription());
            vRow.addElement(v);
        }
        Vector vCol = new Vector();
        vCol.addElement("FunctionId");
        vCol.addElement("Functuion Name");
        vCol.addElement("Description");
        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tblList.setModel(tblModel);
    }

    public void loadAllactifunountDetail() {
        ArrayList<Function> lst = buz.readByAll();
        loadTablefunountDetail(lst);
    }

    private void loadTablefunountDetail(int index) {

        ArrayList<Function> lst = buz.readByAll();
        for (int i = 0; i < lst.size(); i++) {
            if (index != lst.get(i).getFunctionId()) {
                lst.remove(i);
                i--;
            }
        }
        loadAllactifunountDetail();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlList = new javax.swing.JPanel();
        lbllist = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblList = new javax.swing.JTable();
        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        pnlFunctions = new javax.swing.JPanel();
        lblFuncTitle = new javax.swing.JLabel();
        lblFunID = new javax.swing.JLabel();
        lblFunName = new javax.swing.JLabel();
        lblFunDes = new javax.swing.JLabel();
        txtFuncId = new javax.swing.JTextField();
        txtFunname = new javax.swing.JTextField();
        txtDes = new javax.swing.JTextField();
        pnlButton = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        pnlManage = new javax.swing.JPanel();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        pnlList.setLayout(new java.awt.GridBagLayout());

        lbllist.setFont(new java.awt.Font("Tahoma", 0, 18));
        lbllist.setForeground(new java.awt.Color(0, 0, 204));
        lbllist.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbllist.setText("Function List");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 5);
        pnlList.add(lbllist, gridBagConstraints);

        tblList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Function ID", "Function Name", "Description"
            }
        ));
        tblList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlList.add(jScrollPane1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 166;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 10);
        add(pnlList, gridBagConstraints);

        pnlTitle.setLayout(new java.awt.GridBagLayout());

        lblTitle.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblTitle.setForeground(new java.awt.Color(0, 51, 204));
        lblTitle.setText("FUNCTION MANAGEMENT");
        pnlTitle.add(lblTitle, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(15, 10, 10, 10);
        add(pnlTitle, gridBagConstraints);

        pnlFunctions.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Function Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        pnlFunctions.setLayout(new java.awt.GridBagLayout());

        lblFuncTitle.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblFuncTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(20, 20, 5, 20);
        pnlFunctions.add(lblFuncTitle, gridBagConstraints);

        lblFunID.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblFunID.setText("FunctionID ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 5, 5);
        pnlFunctions.add(lblFunID, gridBagConstraints);

        lblFunName.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblFunName.setText("Function Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 5, 5);
        pnlFunctions.add(lblFunName, gridBagConstraints);

        lblFunDes.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblFunDes.setText("Description");
        lblFunDes.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 5, 5);
        pnlFunctions.add(lblFunDes, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 200);
        pnlFunctions.add(txtFuncId, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 200);
        pnlFunctions.add(txtFunname, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 300;
        gridBagConstraints.ipady = 50;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 200);
        pnlFunctions.add(txtDes, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(pnlFunctions, gridBagConstraints);

        pnlButton.setLayout(new java.awt.GridBagLayout());

        btnAdd.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/add2.png"))); // NOI18N
        btnAdd.setText("Add New Function");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlButton.add(btnAdd, gridBagConstraints);

        btnEdit.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/edit.png"))); // NOI18N
        btnEdit.setText("Edit Function");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlButton.add(btnEdit, gridBagConstraints);

        btnDelete.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnDelete.setText("Delete Function");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlButton.add(btnDelete, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.ipady = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        add(pnlButton, gridBagConstraints);

        pnlManage.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Function", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        pnlManage.setLayout(new java.awt.GridBagLayout());

        btnSave.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/save_as.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 60;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 15, 15);
        pnlManage.add(btnSave, gridBagConstraints);

        btnCancel.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 60;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 15, 5);
        pnlManage.add(btnCancel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(pnlManage, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        txtDes.setText("");
        txtFuncId.setText("");
        txtFunname.setText("");
    }//GEN-LAST:event_btnCancelActionPerformed

    private void tblListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListMouseClicked
        // TODO add your handling code here:

        int row = tblList.getSelectedRow();
        if (row < 0) {
            return;
        }
        txtFuncId.setText(tblList.getValueAt(row, 0).toString());
        txtFunname.setText(tblList.getValueAt(row, 1).toString());
        txtDes.setText(tblList.getValueAt(row, 2).toString());
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        btnSave.setEnabled(false);
        loadAllactifunountDetail();
        tblList.updateUI();
    }//GEN-LAST:event_tblListMouseClicked

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        if (!btnEdit.isEnabled()) {

            if (txtFuncId.getText().trim().equals("")
                    || txtFunname.getText().trim().equals("") || txtDes.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Please fill in all the blanks");
                txtFunname.requestFocus();
                return;
            }
            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "New Function", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {

                fun.setFunctionId(Integer.parseInt(txtFuncId.getText()));
                fun.setDescription(txtDes.getText());
                fun.setFunctionName(txtFunname.getText());
                if (buz.create(fun)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtFuncId.setText(null);
                    txtFunname.setText(null);
                    txtDes.setText(null);
                    txtFunname.requestFocus();
                    btnEdit.setEnabled(false);
                    btnDelete.setEnabled(false);
                    loadAllactifunountDetail();
                    this.tblList.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }
        } else {
            txtFuncId.setText(null);
            txtFunname.setText(null);
            txtDes.setText(null);
            txtFunname.requestFocus();
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
        }


    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
         if (btnSave.isEnabled()) {
            JOptionPane.showMessageDialog(this, "This function is being chosen!");
        } else {
            btnDelete.setEnabled(false);
            btnEdit.setEnabled(false);
            btnSave.setEnabled(true);
            btnCancel.setEnabled(true);
            txtFunname.setEditable(true);
            txtFuncId.setEditable(true);
            txtDes.setEditable(true);
            txtDes.setText("");
            txtFuncId.setText("");
            txtFunname.setText("");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
           if (txtFuncId.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please choose a function from the table!");
        } else {
            int funId = Integer.parseInt(txtFuncId.getText());
            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Function", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {
                if (buz.remove(funId)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtFuncId.setText(null);
                    txtFunname.setText(null);
                    txtDes.setText(null);
                    btnEdit.setEnabled(true);
                    btnDelete.setEnabled(true);
                    loadAllactifunountDetail();
                    this.tblList.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }

        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:

          if (txtFuncId.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please choose a  function from the table!");
        } else {
            txtFuncId.setEditable(true);
            txtFunname.setEditable(true);
            txtDes.setEditable(true);

            if (txtDes.getText().trim().equals("")
                    || txtFunname.getText().trim().equals("")
                    || txtFuncId.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Please fill in all the blanks");
                txtFunname.requestFocus();
                return;
            }

            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Update Function", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {
                fun.setFunctionId(Integer.parseInt(txtFuncId.getText()));
                fun.setFunctionName(txtFunname.getText());
                 fun.setDescription(txtDes.getText());

                if (buz.store(fun)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtFuncId.setText(null);
                    txtFunname.setText(null);
                    txtDes.setText(null);
                    btnEdit.setEnabled(true);
                    btnDelete.setEnabled(true);
                    loadAllactifunountDetail();
                    this.tblList.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }
        }
    }//GEN-LAST:event_btnEditActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSave;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblFunDes;
    private javax.swing.JLabel lblFunID;
    private javax.swing.JLabel lblFunName;
    private javax.swing.JLabel lblFuncTitle;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lbllist;
    private javax.swing.JPanel pnlButton;
    private javax.swing.JPanel pnlFunctions;
    private javax.swing.JPanel pnlList;
    private javax.swing.JPanel pnlManage;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JTable tblList;
    private javax.swing.JTextField txtDes;
    private javax.swing.JTextField txtFuncId;
    private javax.swing.JTextField txtFunname;
    // End of variables declaration//GEN-END:variables
}
